package com.hubujun.driverlicense.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.hubujun.driverlicense.DLCore;
import com.hubujun.driverlicense.R;
import com.hubujun.driverlicense.adapter.SectionsPagerAdapter;
import com.hubujun.driverlicense.eventbus.FirstEvent;
import com.hubujun.driverlicense.eventbus.MessageEvent;
import com.hubujun.driverlicense.model.QuestionListModel;
import com.hubujun.driverlicense.network.api.QuestionAPI;
import com.hubujun.driverlicense.util.Ln;
import com.hubujun.driverlicense.util.ToastUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.json.JSONObject;

public class MainActivity extends AppCompatActivity implements Response.Listener, Response.ErrorListener {

    public static Intent intentFor(Context context) {
        Intent builder = new Intent(context, MainActivity.class);

        return builder;
    }

    /**
     * The {@link android.support.v4.view.PagerAdapter} that will provide
     * fragments for each of the sections. We use a
     * {@link FragmentPagerAdapter} derivative, which will keep every
     * loaded fragment in memory. If this becomes too memory intensive, it
     * may be best to switch to a
     * {@link android.support.v4.app.FragmentStatePagerAdapter}.
     */
    private SectionsPagerAdapter mSectionsPagerAdapter;

    /**
     * The {@link ViewPager} that will host the section contents.
     */
    private ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        // Create the adapter that will return a fragment for each of the three
        // primary sections of the activity.
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.container);
        mViewPager.setAdapter(mSectionsPagerAdapter);


        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        EventBus.getDefault().register(this);
//        EventBus.getDefault().post(new FirstEvent("hello"));
        QuestionAPI.instance().requestQuestions(MainActivity.this, MainActivity.this, MainActivity.this);
//        PostTest.requestQuestions(MainActivity.this);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe
    public void onEventMainThread(MessageEvent event) {
        if (event instanceof FirstEvent) {
            String msg = "onEventMainThread收到了消息：" + ((FirstEvent) event).getMsg();
            ToastUtils.showToast(MainActivity.this, msg, Toast.LENGTH_LONG);
        }
    }

    @Override
    public void onResponse(Object response) {
        Ln.d("GetTest onResponse response=%s", response.toString());
        if (response instanceof JSONObject) {
            QuestionListModel questionListModel = new QuestionListModel();
            questionListModel.copyFromJsonObject((JSONObject) response);
            if (questionListModel.result != null) {
                DLCore.getInstance().getAccountStorage().getQuestionStorage().addQuestionList(questionListModel.result);
                mSectionsPagerAdapter.notifyDataSetChanged();
            }
        }
    }

    @Override
    public void onErrorResponse(VolleyError error) {
        Ln.e("GetTest onErrorResponse error message=%s,error=%s", error.getMessage(), error);
    }
}
